﻿Public Class Note

    Private _NoteId As Integer
    Private _Title As String
    Private _Content As String
    Private _UserId As Integer
    Private _Timestamp As DateTime
    Private _NeedsToSave As Boolean

    Public Property NeedsToSave As Boolean
        Get
            Return _NeedsToSave
        End Get
        Private Set(ByVal value As Boolean)
            _NeedsToSave = value
        End Set
    End Property

    Public Property UserId As Integer
        Get
            Return _UserId
        End Get
        Set(ByVal value As Integer)
            _UserId = value
            NeedsToSave = True
        End Set
    End Property

    Public Property Title As String
        Get
            If _Title = vbNullString Then
                Return "New Note"
            Else
                Return _Title
            End If

        End Get
        Set(ByVal value As String)
            _Title = value
            NeedsToSave = True
        End Set
    End Property

    Public Property Timestamp As DateTime
        Get
            Return _Timestamp
        End Get
        Set(ByVal value As DateTime)
            _Timestamp = value
        End Set
    End Property

    Public Property Content As String
        Get
            If _Content = vbNullString Then
                Return "New Note"
            Else
                Return _Content
                NeedsToSave = True
            End If
        End Get
        Set(ByVal value As String)
            _Content = value
        End Set
    End Property

    Public ReadOnly Property Username As String
        Get
            Dim sUser As String = vbNullString

            If _UserId > 0 Then
                Dim oUser As User = User.GetUser(_UserId)
                If Not IsNothing(oUser) Then
                    sUser = oUser.Username
                End If
            End If
            Return sUser
        End Get
    End Property

    Public Property Id As Integer
        Get
            Id = _NoteId
        End Get
        Private Set(ByVal value As Integer)
            _NoteId = value
        End Set
    End Property


    Public Sub Add()
        '***
        _Timestamp = Now()
        _UserId = User.CurrentUser.ID

    End Sub

    Public Sub Save()
        Dim param As IDataParameter
        Dim params As New List(Of IDataParameter)
        Dim ret As Integer

        If Id = 0 Then

            param = DatabaseLayer.DatabaseManager.InputParameter("UserId", DbType.Int32, User.CurrentUser.ID)
            params.Add(param)

            param = DatabaseLayer.DatabaseManager.InputParameter("Title", DbType.String, Title)
            params.Add(param)

            param = DatabaseLayer.DatabaseManager.InputParameter("Content", DbType.String, Content)
            params.Add(param)

            ret = DatabaseLayer.DatabaseManager.StoredProc("uspAddNote", params, User.CurrentUser.ID)
            If ret = 0 Then
                Throw New Exception("Note not saved")
            Else
                Id = ret
                NeedsToSave = False
            End If

        Else

            param = DatabaseLayer.DatabaseManager.InputParameter("NoteId", DbType.Int32, Id)
            params.Add(param)

            param = DatabaseLayer.DatabaseManager.InputParameter("UserId", DbType.Int32, User.CurrentUser.ID)
            params.Add(param)

            param = DatabaseLayer.DatabaseManager.InputParameter("Title", DbType.String, Title)
            params.Add(param)

            param = DatabaseLayer.DatabaseManager.InputParameter("Content", DbType.String, Content)
            params.Add(param)

            ret = DatabaseLayer.DatabaseManager.StoredProc("uspUpdateNote", params, User.CurrentUser.ID)

            If ret = 0 Then

                Throw New Exception("Note not saved")
            Else
                NeedsToSave = False
            End If

        End If

    End Sub

    Public Sub Delete()
        Dim params As New List(Of IDataParameter)
        Dim param As IDataParameter = DatabaseLayer.DatabaseManager.InputParameter("NoteId", DbType.Int32, Id)
        params.Add(param)
        Dim lRet As Integer = DatabaseLayer.DatabaseManager.StoredProc("uspDeleteChildNote", params, User.CurrentUser.ID)
        If lRet = 0 Then
            Throw New Exception("Unable to delete note")
        End If
    End Sub

    Public Shared Function Load(ByVal NoteId As Integer) As Note

        Dim dt As New DataTable
        Dim params As New List(Of IDataParameter)

        Dim param As IDataParameter = DatabaseLayer.DatabaseManager.InputParameter("NoteId", DbType.Int32, NoteId)

        params.Add(param)
        DatabaseLayer.DatabaseManager.StoredProc("uspGetNote", params, dt, User.CurrentUser.ID)
        Select Case True
            Case dt.Rows.Count = 0
                Throw New Exception("Note with id " & NoteId & " not found")

            Case dt.Rows.Count = 1

                Dim oNote As New Note
                oNote.Id = NoteId
                oNote.Content = dt.Rows(0)("content")
                oNote.Timestamp = dt.Rows(0)("Timestamp")
                oNote.Title = dt.Rows(0)("title")
                oNote.UserId = dt.Rows(0)("userid")
                oNote._NeedsToSave = False
                Return oNote
            Case Else
                Throw New Exception("More than one Note with id " & NoteId & " found")

        End Select

    End Function

    Public Sub AbandonChanges()
        NeedsToSave = False
    End Sub

End Class
